/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package com.husong.util.aliwantu;

import com.alibaba.media.MediaConfiguration;
import com.alibaba.media.client.MediaClient;
import com.alibaba.media.client.impl.DefaultMediaClient;
import com.alibaba.media.upload.UploadPolicy;
import com.alibaba.media.upload.UploadTokenClient;
import com.alibaba.media.upload.impl.DefaultUploadTokenClient;

/**
 * @author maxmin
 *
 * @date 2015年9月24日 下午6:30:21
 */
public class AliWanTu {
	private static MediaConfiguration configuration = new MediaConfiguration();
	
	private static final String AK="23205578";
	private static final String SK="302f6ae42b0a40f85e75abfe3b58896a";
	private static final String NAME_SPACE="static";

	
	/**
	 * 广告图路径
	 */
	public static final String ADVERT_DIR="produce.client/adver";
	
	
	public static MediaClient getMediaClient(){
		configuration.setType(MediaConfiguration.TYPE_TOP);
		configuration.setAk(AK);
		configuration.setSk(SK);
		configuration.setNamespace(NAME_SPACE);
		return new DefaultMediaClient(configuration);
	}
	
	public static String getAliWanTuUploadToken(String dir){
		
		configuration.setType(MediaConfiguration.TYPE_TOP);
		configuration.setAk(AK);
		configuration.setSk(SK);
		configuration.setNamespace(NAME_SPACE);
		 
		// 2. 获取鉴权Client
		UploadTokenClient tokenClient = new DefaultUploadTokenClient(configuration);
		 
		// 3. 定义上传策略
		UploadPolicy uploadPolicy = new UploadPolicy();
		uploadPolicy.setDir(dir);
		uploadPolicy.setInsertOnly(UploadPolicy.INSERT_ONLY_NONE);
		uploadPolicy.setExpiration(System.currentTimeMillis() + 3600 * 1000);
		 
		// 4. 获取上传Token
		return tokenClient.getUploadToken(uploadPolicy);
	}
}
